﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Utilities
{
    public class ConfigHelper : AppSettingsReader
    {
        private XmlNode _node;
        private string _cfgFile;
        public string ConfigFile
        {
            get
            {
                return this._cfgFile;
            }
            set
            {
                this._cfgFile = Application.StartupPath + "\\" + value;
            }
        }
        public ConfigHelper()
        {
            this._cfgFile = Application.ExecutablePath + ".Config";
        }
        public string GetValue(string key)
        {
            return Convert.ToString(this.GetValue(key, typeof(string)));
        }
        public new object GetValue(string key, Type sType)
        {
            XmlDocument xmlDocument = new XmlDocument();
            object obj = string.Empty;
            this.LoadDoc(xmlDocument);
            string xpath = key.Substring(0, key.LastIndexOf("//"));
            object result;
            try
            {
                this._node = xmlDocument.SelectSingleNode(xpath);
                if (this._node != null)
                {
                    XmlElement xmlElement = (XmlElement)this._node.SelectSingleNode(key);
                    if (xmlElement != null)
                    {
                        obj = xmlElement.GetAttribute("value");
                    }
                }
                if (sType == typeof(string))
                {
                    result = Convert.ToString(obj);
                }
                else
                {
                    if (sType == typeof(bool))
                    {
                        if (obj.Equals("True") || obj.Equals("False"))
                        {
                            result = Convert.ToBoolean(obj);
                        }
                        else
                        {
                            result = false;
                        }
                    }
                    else
                    {
                        if (sType == typeof(int))
                        {
                            result = Convert.ToInt32(obj);
                        }
                        else
                        {
                            if (sType == typeof(double))
                            {
                                result = Convert.ToDouble(obj);
                            }
                            else
                            {
                                if (sType == typeof(DateTime))
                                {
                                    result = Convert.ToDateTime(obj);
                                }
                                else
                                {
                                    result = Convert.ToString(obj);
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                result = string.Empty;
            }
            return result;
        }
        public bool SetValue(string key, string val)
        {
            XmlDocument xmlDocument = new XmlDocument();
            this.LoadDoc(xmlDocument);
            bool result;
            try
            {
                string text = key.Substring(0, key.LastIndexOf("//"));
                this._node = xmlDocument.SelectSingleNode(text);
                if (this._node == null)
                {
                    result = false;
                }
                else
                {
                    XmlElement xmlElement = (XmlElement)this._node.SelectSingleNode(key);
                    if (xmlElement != null)
                    {
                        xmlElement.SetAttribute("value", val);
                    }
                    else
                    {
                        text = key.Substring(key.LastIndexOf("//") + 2);
                        XmlElement xmlElement2 = xmlDocument.CreateElement(text.Substring(0, text.IndexOf("[@")).Trim());
                        text = text.Substring(text.IndexOf("'") + 1);
                        xmlElement2.SetAttribute("key", text.Substring(0, text.IndexOf("'")));
                        xmlElement2.SetAttribute("value", val);
                        this._node.AppendChild(xmlElement2);
                    }
                    this.saveDoc(xmlDocument, this._cfgFile);
                    result = true;
                }
            }
            catch
            {
                result = false;
            }
            return result;
        }
        private void saveDoc(XmlDocument doc, string docPath)
        {
            if (this._cfgFile.Equals("web.config"))
            {
                return;
            }
            try
            {
                XmlTextWriter xmlTextWriter = new XmlTextWriter(docPath, null);
                xmlTextWriter.Formatting = Formatting.Indented;
                doc.WriteTo(xmlTextWriter);
                xmlTextWriter.Flush();
                xmlTextWriter.Close();
            }
            catch
            {
            }
        }
        public bool RemoveElement(string key)
        {
            XmlDocument xmlDocument = new XmlDocument();
            this.LoadDoc(xmlDocument);
            bool result;
            try
            {
                string xpath = key.Substring(0, key.LastIndexOf("//"));
                this._node = xmlDocument.SelectSingleNode(xpath);
                if (this._node == null)
                {
                    result = false;
                }
                else
                {
                    XmlNode oldChild = this._node.SelectSingleNode(key);
                    this._node.RemoveChild(oldChild);
                    this.saveDoc(xmlDocument, this._cfgFile);
                    result = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                result = false;
            }
            return result;
        }
        private void LoadDoc(XmlDocument doc)
        {
            doc.Load(this._cfgFile);
        }
    }
}
